home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / yuvcodec.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  5.6 KB  |  181 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <crtdbg.h>
  19.  
  20. #include "yuvcodec.h"
  21. #include "VBitmap.h"
  22.  
  23. //#define MY_FOURCC 'cqcv'
  24. #define MY_FOURCC 'BUDV'
  25.  
  26. YUVCodecDriver g_YUVCodecDriver;
  27.  
  28. YUVCodecDriver::~YUVCodecDriver() {
  29. }
  30.  
  31. BOOL YUVCodecDriver::Load(HDRVR hDriver) {
  32.     return TRUE;
  33. }
  34.  
  35. void YUVCodecDriver::Free(HDRVR hDriver) {
  36. }
  37.  
  38. DWORD YUVCodecDriver::Open(HDRVR hDriver, char *szDescription, LPVIDEO_OPEN_PARMS lpVideoOpenParms) {
  39.     return (DWORD)new YUVCodec();
  40. }
  41.  
  42. void YUVCodecDriver::Disable(HDRVR hDriver) {
  43. }
  44.  
  45. void YUVCodecDriver::Enable(HDRVR hDriver) {
  46. }
  47.  
  48. LPARAM YUVCodecDriver::Default(DWORD dwDriverID, HDRVR hDriver, UINT uiMessage, LPARAM lParam1, LPARAM lParam2) {
  49.     return DefDriverProc(dwDriverID, hDriver, uiMessage, lParam1, lParam2);
  50. }
  51.  
  52. /////////////////////////////////////////////////////////////
  53.  
  54. YUVCodec::YUVCodec() {
  55. }
  56.  
  57. YUVCodec::~YUVCodec() {
  58. }
  59.  
  60. LRESULT YUVCodec::Compress(ICCOMPRESS *icc, DWORD cbSize) {
  61.     return ICERR_UNSUPPORTED;
  62. }
  63.  
  64. LRESULT YUVCodec::CompressFramesInfo(ICCOMPRESSFRAMES *icf, DWORD cbSize) {
  65.     return ICERR_UNSUPPORTED;
  66. }
  67.  
  68. LRESULT YUVCodec::CompressGetFormat(BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) {
  69.     return ICERR_UNSUPPORTED;
  70. }
  71.  
  72. LRESULT YUVCodec::CompressGetSize(BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) {
  73.     return ICERR_UNSUPPORTED;
  74. }
  75.  
  76. LRESULT YUVCodec::CompressQuery(BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) {
  77.     return ICERR_UNSUPPORTED;
  78. }
  79.  
  80. LRESULT YUVCodec::DecompressGetFormat(BITMAPINFO *lpbiInput, BITMAPINFO *lpbiOutput) {
  81.     BITMAPINFOHEADER *bmihInput        = &lpbiInput->bmiHeader;
  82.     BITMAPINFOHEADER *bmihOutput    = &lpbiOutput->bmiHeader;
  83.     LRESULT res;
  84.  
  85.     if (!lpbiOutput)
  86.         return sizeof(BITMAPINFOHEADER);
  87.  
  88.     if (ICERR_OK != (res = DecompressQuery(lpbiInput, 0)))
  89.         return res;
  90.  
  91.     bmihOutput->biSize            = sizeof(BITMAPINFOHEADER);
  92.     bmihOutput->biWidth            = bmihInput->biWidth;
  93.     bmihOutput->biHeight        = bmihInput->biHeight;
  94.     bmihOutput->biPlanes        = 1;
  95.     bmihOutput->biBitCount        = 24;
  96.     bmihOutput->biCompression    = BI_RGB;
  97.     bmihOutput->biSizeImage        = ((bmihInput->biWidth*3+3)&-4)*bmihInput->biHeight;
  98.     bmihOutput->biXPelsPerMeter    = bmihInput->biXPelsPerMeter;
  99.     bmihOutput->biYPelsPerMeter    = bmihInput->biYPelsPerMeter;
  100.     bmihOutput->biClrUsed        = 0;
  101.     bmihOutput->biClrImportant    = 0;
  102.  
  103.     return ICERR_OK;
  104. }
  105.  
  106. LRESULT YUVCodec::DecompressEx(ICDECOMPRESSEX *icdex, DWORD cbSize) {
  107.     BITMAPINFOHEADER *bmihInput        = icdex->lpbiSrc;
  108.     BITMAPINFOHEADER *bmihOutput    = icdex->lpbiDst;
  109.     LRESULT res;
  110.     long w0s, h0s, w0d, h0d;
  111.  
  112.     /////////////////////
  113.  
  114.     if (ICERR_OK != (res = DecompressExQuery(icdex, cbSize)))
  115.         return res;
  116.  
  117.     if (icdex->dwFlags & (ICDECOMPRESS_UPDATE))
  118.         return ICERR_BADPARAM;
  119.  
  120.     w0s = icdex->dxSrc==-1 ? bmihInput->biWidth  : icdex->dxSrc;
  121.     h0s = icdex->dySrc==-1 ? bmihInput->biHeight : icdex->dySrc;
  122.     w0d = icdex->dxDst==-1 ? bmihOutput->biWidth  : icdex->dxDst;
  123.     h0d = icdex->dyDst==-1 ? bmihOutput->biHeight : icdex->dyDst;
  124.  
  125.     if (w0s != w0d || h0s != h0d)
  126.         return ICERR_BADPARAM;
  127.  
  128.     if (w0s > bmihInput->biWidth || h0s > bmihInput->biHeight)
  129.         return ICERR_BADPARAM;
  130.  
  131.     if (w0s <= 0 || h0s <= 0)
  132.         return ICERR_OK;
  133.  
  134.     VBitmap(icdex->lpDst, bmihOutput).BitBltFromYUY2(icdex->xDst, icdex->yDst, &VBitmap(icdex->lpSrc, bmihInput), icdex->xSrc, icdex->ySrc, w0s, h0s);
  135. //    VBitmap(icdex->lpDst, bmihOutput).BitBltFromYUY2Fullscale(icdex->xDst, icdex->yDst, &VBitmap(icdex->lpSrc, bmihInput), icdex->xSrc, icdex->ySrc, w0s, h0s);
  136.  
  137.     return ICERR_OK;
  138. }
  139.  
  140. LRESULT YUVCodec::DecompressExQuery(ICDECOMPRESSEX *icdex, DWORD cbSize) {
  141.     BITMAPINFOHEADER *bmihInput        = icdex->lpbiSrc;
  142.  
  143.     // We only accept YUY2!
  144.  
  145.     if (bmihInput->biPlanes != 1) return ICERR_BADFORMAT;
  146.     if (bmihInput->biBitCount != 16) return ICERR_BADFORMAT;
  147.     if (bmihInput->biCompression != '2YUY') return ICERR_BADFORMAT;
  148.  
  149.     if (icdex->lpbiDst) {
  150.         BITMAPINFOHEADER *bmihOutput    = icdex->lpbiDst;
  151.  
  152.         // YUY2 can decompress to RGB16, RGB24, and RGB32.
  153.  
  154.         if (bmihOutput->biPlanes != 1) return ICERR_BADFORMAT;
  155.         if (bmihOutput->biCompression != BI_RGB) return ICERR_BADFORMAT;
  156.         if (bmihOutput->biBitCount != 16 && bmihOutput->biBitCount != 24 && bmihOutput->biBitCount != 32) return ICERR_BADFORMAT;
  157.         if (bmihOutput->biWidth != bmihInput->biWidth) return ICERR_BADFORMAT;
  158.         if (bmihOutput->biHeight != bmihInput->biHeight) return ICERR_BADFORMAT;
  159.     }
  160.  
  161.     _RPT0(0,"--------------Accepted!\n");
  162.  
  163.     return ICERR_OK;
  164. }
  165.  
  166. LRESULT YUVCodec::GetInfo(ICINFO *lpicinfo, DWORD cbSize) {
  167.     if (cbSize < sizeof(ICINFO)) return 0;
  168.  
  169.     lpicinfo->dwSize        = sizeof(ICINFO);
  170.     lpicinfo->fccType        = ICTYPE_VIDEO;
  171.     lpicinfo->fccHandler    = MY_FOURCC;
  172.     lpicinfo->dwFlags        = 0;
  173.     lpicinfo->dwVersion        = 1;
  174.     lpicinfo->dwVersionICM    = ICVERSION;
  175.     wcscpy(lpicinfo->szName, L"VirtualDubYUV");
  176.     wcscpy(lpicinfo->szDescription, L"VirtualDub YUV driver");
  177.  
  178.     return sizeof(ICINFO);
  179. }
  180.  
  181.